package org.example.view;

import org.example.dao.FileClientService;
import org.example.dao.MessageClientService;
import org.example.dao.MessageDao;
import org.example.dao.UserClientService;
import org.example.pojo.MessageCreate;
import org.example.utils.Utility;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class QQView {
    private boolean loop = true;//控制是否显示菜单
    private final UserClientService userClientService = new UserClientService();//用来登录服务器/注册用户
    private final MessageClientService messageClientService = new MessageClientService();//私聊消息的发送
    private final FileClientService fileClientService = new FileClientService();//用于传输文件

    public static void main(String[] args) {
        new QQView().mainMenu();
        System.out.println("系统退出！");
    }

    void mainMenu(){
        while (loop){
            System.out.println("**********欢迎登录论坛聊天室**********");
            System.out.println("\t\t 1 登录系统");
            System.out.println("\t\t 9 退出系统");
            System.out.print("请输入你的选择：");
            //根据用户的输入，来处理不同的逻辑
            //接收用户的键盘输入
            String key = Utility.readString(1);
            switch (key) {
                case "1" -> {
                    System.out.print("请输入用户名：");
                    String username = new Scanner(System.in).nextLine();
                    System.out.print("请输入密码：");
                    String password =new Scanner(System.in).nextLine();
                    //到服务器端去验证该用户是否合法
                    //使用UserClientService类（用户登录、注册）
                    if (userClientService.checkUser(username, password)) {
                        System.out.println("**********欢迎 " + username + " **********");
                        //进入到二级菜单
                        while (loop) {
                            System.out.println("\n**********论坛聊天室二级菜单（用户 " + username + "）**********");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 5 查看聊天记录");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请输入你的选择：");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    //System.out.println("显示在线用户列表");
                                    //写一个方法来获取在线用户列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.print("请输入要群发的消息内容：");
                                    String s = Utility.readString(100);
                                    //调用一个方法，将消息封装成Message对象，发送给服务器端
                                    messageClientService.sendMessageToAll(s, username);
                                    break;
                                case "3":
                                    System.out.print("请输入想聊天的用户名（在线）：");
                                    String getterId = new Scanner(System.in).nextLine();
                                    System.out.print("请输入要发送的消息内容：");
                                    String content =new Scanner(System.in).nextLine();
                                    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                                    Date date = new Date(System.currentTimeMillis());
                                    String settime = formatter.format(date);
                                    String gettime = formatter.format(date);
                                    try {
                                        new MessageDao().insertMessage(username,password,settime,getterId,gettime,content);
                                    } catch (SQLException e) {
                                        throw new RuntimeException(e);
                                    }
                                    //编写一个方法，将消息发送给服务器端
                                    messageClientService.sendMessageToOne(content, username, getterId);
                                    System.out.println("私聊消息");
                                    break;
                                case "4":
                                    System.out.print("请输入你想发送文件的用户（在线）：");
                                    getterId = Utility.readString(50);
                                    System.out.print("请输入你要发送文件的路径（形式 D:\\xx.jpg）：");
                                    String src = Utility.readString(100);
                                    System.out.print("请输入把文件发送到对方的路径（形式 D:\\xx.jpg）：");
                                    String dest = Utility.readString(100);
                                    fileClientService.sendFileToOne(src, dest, username, getterId);
                                    break;
                                case "5":
                                    try {
                                        List<MessageCreate> list = new MessageDao().messageCreates(username);
                                        System.out.println("\t\t\t\t\t\t\t\t\t"+"社区论坛聊天消息内容");
                                        System.out.println("用户名"+"\t\t\t\t"+"发送时间"+"\t\t\t\t\t\t"+"接收者"+"\t\t\t\t"+"接收时间"+"\t\t\t\t\t\t"+"消息内容");
                                        for(MessageCreate i:list){
                                            System.out.println(i.getUsername()+"\t\t\t"+i.getSendTime()+"\t\t\t"+i.getGetter()+"\t\t\t"+i.getGetTime()+"\t\t\t"+i.getContent());
                                        }
                                    } catch (Exception e) {
                                        throw new RuntimeException(e);
                                    }
                                    break;
                                case "9":
                                    //调用一个方法，给服务器端发送一个退出系统的Message
                                    userClientService.logout();
                                    loop = false;
                                    break;
                            }
                        }
                    } else {
                        System.out.println("**********登录失败**********");
                    }
                }
                case "9" -> {
                    loop = false;
                    System.out.println("退出系统");
                }
            }
        }
    }
}
